﻿using System;
using System.Collections;
using System.Text;
using System.Threading;
using Framework.MQS;
using log4net;

namespace Framework.Logger
{
    /// <summary>
    /// 日志操作
    /// </summary>
    public class LogManager
    {
        private LogManager()
        { }
        private static readonly LogManager instance = new LogManager();
        public static LogManager getInstance()
        {
            return instance;
        }
        /// <summary>
        /// 启动监控日志队列服务
        /// </summary>
        public static void StartMonitorLogQueue()
        {
            Thread thread = new Thread(DealLog);
            thread.Start();
        }

        private static void DealLog()
        {
            var queue = new AliyunQueue();
            while (true)
            {
                lock (queue)
                {
                    if (queue.IsData)
                    {
                        queue.Pop(data =>
                        {
                            try
                            {
                                instance.WriteInfoLog("InfoLog", data);
                                return true;
                            }
                            catch (Exception exception)
                            {
                                return false;
                            }
                        });
                    }
                    else
                    {
                        Thread.Sleep(1000);
                    }
                }

            }
        }
        #region 写日志方法
        /// <summary>
        /// 写错误日志文件
        /// SF 20140625
        /// </summary>
        /// <param name="logPath">日志文件夹</param>
        /// <param name="file">文件内容</param>
        public void WriteErrorLog(string logPath, string file)
        {
            ILog log = log4net.LogManager.GetLogger(logPath);
            log.Error(file);
        }

        /// <summary>
        /// 一般日志记录
        /// </summary>
        /// <param name="logPath"></param>
        /// <param name="contents"></param>
        public void WriteInfoLog(string logPath, string contents)
        {
            ILog log = log4net.LogManager.GetLogger(logPath);
            log.Info(contents);
        }

        /// <summary>
        /// 记录日志
        /// SJ 2015-07-23
        /// </summary>
        /// <param name="logname"></param>
        /// <param name="logcontent"></param>
        public void WriteLog(string logPath, string contents)
        {
            /*
                1、FATAL (致命错误)
                2、ERROR(一般错误)
                3、WARN(警告)
                4、INFO(一般信息)
                5、DEBUG(调试信息) 
             */
            ILog log = log4net.LogManager.GetLogger(logPath);
            if (log.IsErrorEnabled)
            {
                log.Error(contents);
            }
            else if (log.IsInfoEnabled)
            {
                log.Info(contents);
            }
            else if (log.IsDebugEnabled)
            {
                log.Debug(contents);
            }
            else if (log.IsFatalEnabled)
            {
                log.Fatal(contents);
            }
            else
            {
                log.Info(contents);
            }
        }


        /// <summary>
        /// 写异常日志
        /// </summary>
        /// <param name="logPath"></param>
        /// <param name="e"></param>
        public void WriteException(string logPath, Exception e)
        {
            if (e != null)
            {
                string errors = String.Format("日期:{0}\r\n{1}\r\n", DateTime.Now.ToString(), e.Message + ":" + e.Source + ":" + e.StackTrace);
                WriteErrorLog(logPath, errors);
            }
        }

        /// <summary>
        /// 写API 信息日志
        /// </summary>
        /// <param name="hash">参数</param>
        /// <param name="url">请求连接</param>
        /// <param name="fun">请求方法</param>
        /// <param name="msg">处理结果</param>
        public void WriteAPIInfoLog(Hashtable hash, string url, string fun, string msg)
        {
            ILog log = log4net.LogManager.GetLogger("");

            var sbLog = new StringBuilder();
            //请求信息
            sbLog.AppendFormat("请求地址：{0}  方法：{1}\r\n", url, fun);

            //参数
            var sb_param = new StringBuilder();
            sb_param.Append("{\r\n");
            foreach (var para in hash.Keys)
            {
                var val = hash[para];
                sb_param.AppendFormat("    {0}:{1},\r\n", para, val);
            }
            sb_param.Append("}\r\n");
            sbLog.AppendFormat("参数：{0}", sb_param);

            //处理结果
            sbLog.AppendFormat("处理结果：{0}", msg);

            log.Info(sbLog);
        }
        /// <summary>
        /// 写API 信息日志
        /// </summary>
        /// <param name="data">数据</param>
        public void WriteRequestLog(string data)
        {
            ILog log = log4net.LogManager.GetLogger("RequestLog");

            var sbLog = new StringBuilder();

            sbLog.AppendFormat("请求信息：{0}", data);

            log.Info(sbLog);
        }
        #endregion
    }
}
